package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IDmChannelRetentDAO;
import org.jsbd.boss.dao.INewStatisticsDao;
import org.jsbd.boss.dao.IRetentStatisticsDao;
import org.jsbd.boss.domian.UserUpgradeVo;
import org.jsbd.boss.domian.statistics.AddStatisticsVo;
import org.jsbd.boss.domian.statistics.ImsiretentResultVo;
import org.jsbd.boss.service.IUserUpgradeService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



@Service("userUpgradeService")
public class UserUpgradeServiceImpl implements IUserUpgradeService{
	public static final Logger LOGGER = LoggerFactory.getLogger(UserUpgradeServiceImpl.class);

	@Autowired
	private IRetentStatisticsDao retentStatisticsDao;
	@Autowired
	private IDmChannelRetentDAO dmChannelRetentDao;
	@Autowired
	private INewStatisticsDao newStatisticsDao;
	
	@Override
	public Map<String, Object> getUnUpgrageList(UserUpgradeVo userUpgradeVo) {
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("total", retentStatisticsDao.selectUnUpgrageListCount(userUpgradeVo));
		
		List<UserUpgradeVo> vos = retentStatisticsDao.selectUnUpgrageList(userUpgradeVo);
		results.put("rows", vos);
		return results;
	}

	@Override
	public void updateJZByMark(String mark) {
		String tableName = "qy_crack_dm_channel_imsiretent_";
		List<String> tables = dmChannelRetentDao.getAllTableByTableName(tableName);
		String date;
		ImsiretentResultVo imsiretent;
		for (String table : tables) {
			 try {
				imsiretent = new ImsiretentResultVo();
				 date =  table.substring(table.lastIndexOf("_") + 1);
				 String rd = DateUtil.transferDateToString(DateUtil.getCurrentYmdHmsDate(date, "yyyyMMdd"), "yyyy-MM-dd");
				 imsiretent.setMark(mark);
				 imsiretent.setTable(table);
				 List<ImsiretentResultVo>  imsiRetentVos = dmChannelRetentDao.selectByMark(imsiretent);//根据table和mark 查找对应mark的数量
				 for (ImsiretentResultVo vo:imsiRetentVos) {
					 vo.setRd(rd);
				 }
				 retentStatisticsDao.updateByMarkAndRd(imsiRetentVos);
			} catch (Exception e) {
				 LOGGER.error("根据mark校准在执行表" + table + "统计操作时失败" ,  e);
			} 
		}
	}
    
	private final String tableName = "qy_crack_dm_channel_imsiretent_";
	@Override
	public void updateJZByRd(String startTime, String endTime, String mark) {
		
		String startTimeyyyyMMdd = startTime.replaceAll("-", "");
		String endTimeyyyyMMdd = endTime.replaceAll("-", "");
		Set<String> datesBetweenTwoDate = DateUtil.getDatesBetweenTwoDate(startTimeyyyyMMdd, endTimeyyyyMMdd);
		ImsiretentResultVo imsiretent;
		for (String date : datesBetweenTwoDate) {
			 try {
				 imsiretent = new ImsiretentResultVo();
				 imsiretent.setTable(tableName + date);
				 if (StringUtils.isNotBlank(mark)) {
					 imsiretent.setMark(mark); 
				 }
				 List<ImsiretentResultVo>imsiRetentVos = dmChannelRetentDao.selectByMark(imsiretent);//根据table和mark 查找对应mark的数量
				 String rd = DateUtil.transferDateToString(DateUtil.getCurrentYmdHmsDate(date, "yyyyMMdd"), "yyyy-MM-dd");
				 for (ImsiretentResultVo vo:imsiRetentVos) {
					 vo.setRd(rd);
				 }
				 retentStatisticsDao.updateByMarkAndRd(imsiRetentVos);
			} catch (Exception e) {
				LOGGER.error("根据日期校准在执行表" + tableName + date + "统计操作时失败" ,  e);
			} 
		}
	}

	@Override
	public Map<String, Object> getAddJZList(AddStatisticsVo addStatisticsVo) {
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("total", newStatisticsDao.selectAddStatisticsCount(addStatisticsVo));
		
		List<AddStatisticsVo> vos = newStatisticsDao.selectAddStatisticsList(addStatisticsVo);
		for (AddStatisticsVo vo : vos) {
			if (vo.getModifyTime() != null) {
				vo.setModifyDate(org.jsbd.boss.util.DateUtil.getYmdhmsDateStr(vo.getModifyTime()));	
			}
		}
		results.put("rows", vos);
		return results;
	}

	@Override
	public void updateAddJZByDate(String startTime, String endTime, String mark) {
		//拿到所有rd 和  mark 放入list 方便排重
		Map<String, Object> param = new HashMap<>();
		param.put("startTime", startTime);
		param.put("endTime", endTime);
		param.put("mark", mark);
		List<AddStatisticsVo> vos =  newStatisticsDao.getAllStatisticsList(param);
		List<String> rdMarkLists = new ArrayList<>();
		for (AddStatisticsVo vo : vos) {
			rdMarkLists.add(vo.getMark() + vo.getRd().replaceAll("-", ""));
		}
		//拿到需要检查校准的数据
		String startTimeyyyyMMdd = startTime.replaceAll("-", "");
		String endTimeyyyyMMdd = endTime.replaceAll("-", "");
		Set<String> datesBetweenTwoDate = DateUtil.getDatesBetweenTwoDate(startTimeyyyyMMdd, endTimeyyyyMMdd);
		ImsiretentResultVo imsiretent;
		AddStatisticsVo insertBean;
		List<AddStatisticsVo> insertList;//批量插入的list
		for (String date : datesBetweenTwoDate) {
			 try {
				 insertList = new ArrayList<>();
				 imsiretent = new ImsiretentResultVo();
				 imsiretent.setTable(tableName + date);
				 if (StringUtils.isNotBlank(mark)) {
					 imsiretent.setMark(mark); 
				 }
				 List<ImsiretentResultVo> imsiRetentVos = dmChannelRetentDao.selectByMark(imsiretent);//根据table和mark 查找对应mark的数量
				 for (ImsiretentResultVo imsiRetentVo : imsiRetentVos) {
					 if (!rdMarkLists.contains(imsiRetentVo.getMark() + date)) {
						 String rd = DateUtil.transferDateToString(DateUtil.getCurrentYmdHmsDate(date, "yyyyMMdd"), "yyyy-MM-dd");
						 insertBean = new  AddStatisticsVo();
						 insertBean.setRd(rd);
						 insertBean.setMark(imsiRetentVo.getMark());
						 insertBean.setExecuteNum(imsiRetentVo.getTotal());
						 insertBean.setPhoneNum(imsiRetentVo.getPhoneNum());
						 insertList.add(insertBean);
					 }
				 }
				 newStatisticsDao.insertStatistics(insertList);
			 } catch (Exception e) {
				LOGGER.error("根据日期校准新增数据:" + tableName + date + "统计操作时失败" ,  e);
			} 
		}
	}
	
}
